//
//  MediaInfoList.mm
//  MediaInfoGUI
//
//  Created by Olivier Larivain on 2/13/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "MediaInfoList.h"
#import "MediaInfo/MediaInfoList.h"

using namespace MediaInfoLib;

@interface MediaInfoList(hidden)
  MediaInfoLib::MediaInfoList *mediaInfoList;
@end

@implementation MediaInfoList

- (id)init
{
    self = [super init];
    if (self)
    {
    mediaInfoList = new MediaInfoLib::MediaInfoList();
    }
    return self;
}

- (void)dealloc
{
  [super dealloc];
  delete  mediaInfoList;
}

- (int) open:(NSString*) files option:(OpenOption) option
{
  String str = [files UTF8String];
  fileoptions_t miOption;
  switch(option)
  {
    case None:
      miOption = FileOption_Nothing;
      break;
    case NoRecursive:
      miOption = FileOption_NoRecursive;
      break;
    case CloseAll:
      miOption = FileOption_CloseAll;
      break;
    case Max:
      miOption = FileOption_Max;
      break;
  }
  int returnValue = mediaInfoList->Open(str,miOption);
  return returnValue;
}

- (NSString *) inform: (int) fileIndex
{
  String str = mediaInfoList->Inform(fileIndex);
  NSString *returnString = [[NSString alloc] initWithCString:str.c_str() encoding:NSASCIIStringEncoding];
  return returnString;
}



- (NSString*) getGeneralInfo:(int) fileIndex streamId:(int) streamId parameter:(NSString*) parameter
{
  String str = mediaInfoList->Get(fileIndex, Stream_General, streamId, [parameter UTF8String]);
  NSString *returnString = [[NSString alloc] initWithCString:str.c_str() encoding:NSASCIIStringEncoding];
  return returnString;
}


- (NSString*) getVideoInfo:(int) fileIndex streamId:(int) streamId parameter:(NSString*) parameter
{
  String str = mediaInfoList->Get(fileIndex, Stream_Video, streamId, [parameter UTF8String]);
  NSString *returnString = [[NSString alloc] initWithCString:str.c_str() encoding:NSASCIIStringEncoding];
  return returnString;
}

- (NSString*) getAudioInfo:(int) fileIndex streamId:(int) streamId parameter:(NSString*) parameter
{
  String str = mediaInfoList->Get(fileIndex, Stream_Audio, streamId, [parameter UTF8String]);
  NSString *returnString = [[NSString alloc] initWithCString:str.c_str() encoding:NSASCIIStringEncoding];
  return returnString;
}


- (NSString*) getTextInfo:(int) fileIndex streamId:(int) streamId parameter:(NSString*) parameter
{
  String str = mediaInfoList->Get(fileIndex, Stream_Text, streamId, [parameter UTF8String]);
  NSString *returnString = [[NSString alloc] initWithCString:str.c_str() encoding:NSASCIIStringEncoding];
  return returnString;
}

-(NSString*) getVideoCodecInfo:(int) fileIndex
{
  String str = mediaInfoList->Get(fileIndex, Stream_Video, 0, "StreamKind", Info_Text)+"_Format_WithHint_List";
  NSString *videoCodecInfo = [[NSString alloc] initWithCString: mediaInfoList->Get(fileIndex, Stream_General, 0, str).c_str() encoding:NSASCIIStringEncoding];
  return videoCodecInfo;
}

-(NSString*) getAudioCodecInfo:(int) fileIndex
{
  
  String str = mediaInfoList->Get(fileIndex, Stream_Audio, 0, "StreamKind", Info_Text)+"_Format_WithHint_List";
  NSString *videoCodecInfo = [[NSString alloc] initWithCString: mediaInfoList->Get(fileIndex, Stream_General, 0, str).c_str() encoding:NSASCIIStringEncoding];
  return videoCodecInfo;
}

-(NSString*) getTextCodecInfo:(int) fileIndex
{
  
  String str = mediaInfoList->Get(fileIndex, Stream_Text, 0, "StreamKind", Info_Text)+"_Format_WithHint_List";
  NSString *videoCodecInfo = [[NSString alloc] initWithCString: mediaInfoList->Get(fileIndex, Stream_General, 0, str).c_str() encoding:NSASCIIStringEncoding];
  return videoCodecInfo;
}

- (NSString*) getGeneralMetaData:(int) fileIndex
{
  size_t MI_Pos=94;
  size_t MI_Max=mediaInfoList->Count_Get(fileIndex, Stream_General, 0);
  int nbLines=0;
  String str;
  while(MI_Pos<MI_Max)
  {
    String Z1=mediaInfoList->Get(fileIndex, Stream_General, 0, MI_Pos);
    String Options=mediaInfoList->Get(fileIndex, Stream_General, 0, MI_Pos, Info_Options);
    if (Options.size()>InfoOption_ShowInInform && Options[InfoOption_ShowInInform]==_T('Y') && Z1.size()>0)
    {
      String Z2=mediaInfoList->Get(fileIndex, Stream_General, 0, MI_Pos, Info_Name_Text);
      if (Z2.size()==0)
        Z2=mediaInfoList->Get(fileIndex, Stream_General, 0, MI_Pos, Info_Name);
      Z2+=": ";
      Z2+=Z1;
      if(nbLines > 0)
      {
        str+="<br/>";  
      }
      str+=Z2;
      nbLines++;
    }
    MI_Pos++;
  }
  return [[NSString alloc] initWithCString:str.c_str() encoding:NSASCIIStringEncoding];
}

- (int) generalStreams: (int) fileIndex
{
  return mediaInfoList->Count_Get(fileIndex, Stream_General);
}

- (int) videoStreams: (int) fileIndex;
{
  return mediaInfoList->Count_Get(fileIndex, Stream_Video);
}

- (int) audioStreams: (int) fileIndex;
{
  return mediaInfoList->Count_Get(fileIndex, Stream_Audio);
}

- (int) textStreams: (int) fileIndex;
{
  return mediaInfoList->Count_Get(fileIndex, Stream_Text);
}

- (void) setInfoLevel: (NSString*) level
{
  mediaInfoList->Option("Inform", [level UTF8String]);
}

-(void) close
{
  mediaInfoList->Close();
}

@end
